import 'package:flutter/material.dart';
import 'package:flutter_app/util/DataUtil.dart';

import 'AlignCenterWidget.dart';
import 'FlexWidget.dart';
import 'RowColumnWidget.dart';
import 'StackPositionedWidget.dart';
import 'WrapFlowWidget.dart';

/// listview列表
class LayoutListWidget extends StatelessWidget {
  LayoutListWidget({
    Key ? key,
    @required this.text, // 接收一个text参数
  }) : super(key: key);
  final String ? text;

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: AppBar(
        title: Text(text??""),
      ),
      body: BaseListPage(),
    );
  }
}

class BaseListPage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<BaseListPage> {
  var list = DataUtil.getLayoutTitle();

  @override
  Widget build(BuildContext context) {
    return ListView.builder(
      itemCount: list.length,
      itemBuilder: _buildItem,
      itemExtent: 60,
    );
  }
}

Widget _buildItem(BuildContext context, int index) {
  var list = DataUtil.getLayoutTitle();
  return Container(
    color: Colors.white,
    height: double.infinity,
    child: GestureDetector(
      child: Container(
        margin: EdgeInsets.all(10),
        padding: EdgeInsets.all(10),
        color: Colors.cyan,
        child: Text(
          list[index],
          style: TextStyle(
            color: Colors.white,
          ),
        ),
      ),
      onTap: () {
        toArount(index, context);
      },
    ),
  );
}

void toArount(int index, BuildContext context) {
  switch (index) {
    case 0:
      goTo(context, RowColumnWidget(text: "线性布局"));
      break;
    case 1:
      goTo(context, FlexWidget(text: "弹性布局"));
      break;
    case 2:
      goTo(context, WrapFlowWidget(text: "流式布局"));
      break;
    case 3:
      goTo(context, StackPositionedWidget(text: "层叠布局"));
      break;
    case 4:
      goTo(context, AlignCenterWidget(text: "对齐与先相对定位[Algin]"));
      break;
  }
}

void goTo(BuildContext context, Widget widget) {
  Navigator.push(context, MaterialPageRoute(builder: (context) {
    return widget;
  }));
}
